# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=spirv-tools
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2020.4
pkgrel=1
pkgdesc="API and commands for processing SPIR-V modules (mingw-w64)"
arch=('any')
url="https://www.khronos.org/vulkan/"
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/KhronosGroup/SPIRV-Tools/archive/v${pkgver}.tar.gz"
        001-cmake-files-location.patch)
sha512sums=('ab694797e69fa095fe60c7f9bb832ccbc4730f487f53d1c8b30eabadb9cf714bc4720cbf4d811e85a779324b1170291bc0dfccf0c678d51617bd82e6b32b74b4'
            '8cbad8289e5f29c621cf225756e69a7784eba005998a041ae3d81cb017582956ec2d07d163ea7a3de38f3f7bd34900509511a8104d2a2d196da23d87c8e35a58')

prepare() {
  cd SPIRV-Tools-${pkgver}
  patch -p1 -i ${srcdir}/001-cmake-files-location.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir -p "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  msg2 "Building shared library"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DSPIRV_WERROR=OFF \
      -DBUILD_SHARED_LIBS=ON \
      -DSPIRV-Headers_SOURCE_DIR=${MINGW_PREFIX} \
      ../SPIRV-Tools-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./

  msg2 "Building static library"
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DSPIRV_WERROR=OFF \
      -DBUILD_SHARED_LIBS=OFF \
      -DSPIRV-Headers_SOURCE_DIR=${MINGW_PREFIX} \
      ../SPIRV-Tools-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  cd "${srcdir}"/build-${CARCH}-shared
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  cd "${srcdir}"/build-${CARCH}-static
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -m644 ${srcdir}/SPIRV-Tools-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"

  local _WIN_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${_WIN_PREFIX}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
